<template>
  <div class="app-container">
    <!--查询表单-->
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item>
        <el-input v-model="bookQuery.name" placeholder="图书名称"/>
      </el-form-item>

      <el-form-item>
        <el-select v-model="bookQuery.type" clearable placeholder="图书类型">
          <el-option :value="1" label="儿童类"/>
          <el-option :value="2" label="专业类"/>
          <el-option :value="3" label="历史类"/>
          <el-option :value="4" label="修养类"/>
        </el-select>
      </el-form-item>

      <el-form-item label="创建时间">
        <el-date-picker
          v-model="bookQuery.createTimeBegin"
          type="datetime"
          placeholder="选择开始时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          default-time="00:00:00"
        />
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="bookQuery.createTimeEnd"
          type="datetime"
          placeholder="选择截止时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          default-time="00:00:00"
        />
      </el-form-item>

      <el-row style="display:flex">
        <el-button type="primary" icon="el-icon-search" @click="findBooksByConditionPage()">查询</el-button>
        <el-button type="info" icon="el-icon-delete" @click="resetData()">清空</el-button>
        <el-button type="danger" icon="el-icon-delete" @click="deleteBatchByIds()">批量删除</el-button>
      </el-row>
    </el-form>

    <br/>
    <!-- 表格 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      stripe
      border
      style="width: 100%;margin-top: 10px;"
      fit
      highlight-current-row
      @selection-change="handleSelectionChange">

      <el-table-column type="selection"/>

      <el-table-column
        fixed
        label="序号"
        width="70"
        align="center">
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>

      <el-table-column prop="name" label="书名" width="120"/>
 
      <el-table-column prop="intro" label="图书简介" width="300"/>

      <el-table-column label="图书类型" width="120">
        <template slot-scope="scope">
          {{ scope.row.type === 1 ? '儿童类' : (scope.row.type === 2 ? '专业类' : 
            (scope.row.type === 3 ? '历史类' : '修养类')) }}
        </template>
      </el-table-column>

      <el-table-column prop="price" label="图书售价" width="100"/>

      <el-table-column prop="author" label="图书作者" width="150"/>

      <el-table-column prop="createTime" label="添加时间" width="180"/>

      <el-table-column prop="updateTime" label="修改时间" width="180"/>

      <el-table-column fixed="right" label="操作" width="120" align="center">
        <template slot-scope="scope">
            <router-link :to="'/book/edit/' + scope.row.id">
                <el-button type="primary" icon="el-icon-edit" circle></el-button>
            </router-link>&nbsp;
            <el-button type="danger" icon="el-icon-delete" circle @click="deleteBookById(scope.row.id)"></el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      :current-page="page"
      :page-size="limit"
      :total="total"
      style="padding: 30px 0; text-align: center;"
      layout="total, prev, pager, next, jumper"
      @current-change="findBooksByConditionPage"
      @size-change="changeSize"
    />
  </div>
</template>
<script>
import book from '@/api/library/book'

export default {
    data() { //定义变量和初始值
        return {
          list: null, //查询之后接口返回集合
          page: 1, //当前页
          limit: 10, //每页记录数
          total: 0, //总记录数
          bookQuery: {}, //条件封装对象
          multipleSelection: [] //批量删除选中的记录列表
        }
    },
    created() { //页面渲染之前执行，一般调用methods定义的方法
        this.findBooksByConditionPage() 
    },
    methods: {
          //当页码发生改变的时候
        changeSize(size) {
            console.log(size)
            this.limit = size
            this.findBooksByConditionPage(1)
        },
        //条件查询图书，带分页
        findBooksByConditionPage(page=1) {
            this.page = page
            book.findBooksByConditionPage(this.page, this.limit, this.bookQuery)
                .then(response => { //请求成功
                    this.list = response.data.records
                    this.total = response.data.total
                    console.log(this.list)   
                    console.log(this.total)
                })
        },
        //清空查询条件，刷新列表数据
        resetData() {
            this.bookQuery = {}
            this.findBooksByConditionPage()
        },
        //根据id逻辑删除图书
        deleteBookById(id) {
            this.$confirm('此操作将永久删除图书记录，是否继续？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                book.deleteBookById(id).then(response => {
                    this.$message({
                        type: 'success',
                        message: '删除成功！'
                    })
                    //删除之后，刷新列表数据
                    this.findBooksByConditionPage()
                })
            })
        },
        //批量删除图书
        deleteBatchByIds() {
            if (this.multipleSelection.length === 0) {
                this.$message.warning('请选择要删除的记录！')
                return
            }
            this.$confirm('此操作将永久删除这些记录，是否继续？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                var ids = []
                this.multipleSelection.forEach(item => {
                    ids.push(item.id)
                })
                return book.deleteBatchByIds(ids)
            }).then(response => {
                this.$message.success(response.message || '删除成功')
                this.findBooksByConditionPage(this.page)
            })
        },
        //当多选选项发生变化时调用
        handleSelectionChange(selection) {
            console.log(selection)
            this.multipleSelection = selection
        }
    }
}
</script>